/// @tags: DPonTree
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

int const N = 2e3 + 5;

int e_cnt;
int val[N], heads[N], siz[N], f[N], sum[N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

void solve(int u, int fa) {
  siz[u] = val[u], sum[u] = 0;
  int maxx = 0;
  for (int i = heads[u], v; i; i = e[i].nxt) {
    if ((v = e[i].v) != fa) {
      solve(v, u);
      siz[u] += siz[v];
      sum[u] += (sum[v] += siz[v]);
      if (sum[v] > sum[maxx]) maxx = v;
    }
  }
  sum[maxx] * 2 <= sum[u]
      ? f[u] = sum[u] / 2
      : f[u] = sum[u] - sum[maxx] + min(f[maxx] * 2, (sum[maxx] * 2 - sum[u])) / 2;
}

inline int main() {
  static char s[N];
  int n;
  cin >> n >> (s + 1);
  for (int i = 1; i <= n; ++i) val[i] = s[i] - '0';
  for (int i = 1, u, v; i < n; ++i) {
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    solve(i, i);
    if (f[i] * 2 == sum[i]) ans = min(ans, f[i]);
  }
  cout << (ans == 0x3f3f3f3f ? -1 : ans);
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("AT4995 [AGC034E] Complete Compress.in", "r", stdin);
  freopen("AT4995 [AGC034E] Complete Compress.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
